<?php

namespace app\channel\controller\api;
use app\channel\ApiController;

use app\channel\model\ReportLogModel;
use app\channel\model\ProxyUsersModel;
use app\member\model\UsersModel;
/*------------------------------------------------------ */
//-- 维权相关API
/*------------------------------------------------------ */

class Report extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new ReportLogModel();
    }

    /*------------------------------------------------------ */
    //-- 获取举报基础信息
    /*------------------------------------------------------ */
    public function getBaseInfo(){
        //获取同级代理
        $where[] = ['pu.proxy_id','=',$this->userInfo['proxy_id']];
        $where[] = ['pu.status','=',1];
        $where[] = ['pu.user_id','<>',$this->userInfo['user_id']];
        $data['sameLevelUser'] = (new ProxyUsersModel)->alias('pu')->join("users u", 'pu.user_id=u.user_id', 'left')->where($where)->field('u.user_id,u.real_name')->select()->toArray();
        $data['reportType'] = $this->Model->reportType;
        return $this->success($data);
    }
    /*------------------------------------------------------ */
    //-- 提交举报基础信息
    /*------------------------------------------------------ */
    public function post(){
        $report_type = input('report_type',0,'intval');
        if ($report_type < 1){
            return $this->error('请选择举报类型.');
        }
        $report_uid = input('report_uid',0,'intval');
        if ($report_uid < 1){
            return $this->error('请选择被举报人.');
        }
        $fileList = input('fileList','','trim');
        if (empty($fileList)){
            return $this->error('请上传相关证据图片.');
        }
        $description = input('description','','trim');
        if (strlen($description) < 10){
            return $this->error('请填写您的详细描述，长度不能小于10个字符.');
        }
        $file_path = config('config._upload_').'report_img/'.date('m');
        $imgs = [];
        $fileList = explode(',',$fileList);
        foreach ($fileList as $file){
            $imgs[] = copyFile($file,$file_path);
        }
        $inData['user_id'] = $this->userInfo['user_id'];
        $inData['report_type'] = $report_type;
        $inData['report_uid'] = $report_uid;
        $inData['description'] = $description;
        $inData['imgs'] = join(',',$imgs);
        $inData['add_time'] = time();
        $res = $this->Model->save($inData);
        if ($res < 1){
            foreach ($imgs as $img){
                @unlink('.'.$img);
            }
            return $this->error('处理失败，请重试.');
        }
        foreach ($fileList as $file){
            @unlink('.'.$file);
        }
        return $this->success();
    }
    /*------------------------------------------------------ */
    //-- 获取列表
    /*------------------------------------------------------ */
    public function getList()
    {
        $where[] = ['user_id','=',$this->userInfo['user_id']];
        $data = $this->getPageList($this->Model, $where,'log_id,status,report_type,report_uid,add_time,description',6);
        $status = $this->Model->status;
        $reportType = $this->Model->reportType;
        $UsersModel = new UsersModel();
        foreach ($data['list'] as $key=>$row){
            $row['status'] = $status[$row['status']];
            $row['report_type'] = $reportType[$row['report_type']];
            $row['_add_time'] = dateTpl($row['add_time']);
            $row['report_real_name'] = $UsersModel->where('user_id',$row['report_uid'])->value('real_name');
            $data['list'][$key] = $row;
        }
        return $this->success($data);
    }
    /*------------------------------------------------------ */
    //-- 获取详情
    /*------------------------------------------------------ */
    public function getInfo()
    {
        $log_id = input('log_id','0','intval');
        if ($log_id < 1){
            return $this->error('传参错误.');
        }
        $where[] = ['log_id','=',$log_id];
        $where[] = ['user_id','=',$this->userInfo['user_id']];
        $row = $this->Model->where($where)->find();
        if (empty($row)){
            return $this->error('没有找到相关信息.');
        }
        $row['_status'] = $this->Model->status[$row['status']];
        $row['_report_type'] = $this->Model->reportType[$row['report_type']];
        $row['_add_time'] = dateTpl($row['add_time']);
        $row['report_real_name'] = (new UsersModel)->where('user_id',$row['report_uid'])->value('real_name');
        $row['imgs'] = explode(',',$row['imgs']);
        $data = $row->toArray();
        return $this->success($data);
    }
}

